package com.xuan.dao;
import com.xuan.model.Game;
import com.xuan.model.Order_details;
import com.xuan.model.Section;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class Order_DetailsDao extends BaseDao{

    public List<Order_details> getAll (int size,int page) {
        List<Order_details> list = new LinkedList<>();
        String sql = "select order_id, accompanying_play_id, play_time, order_price" +
                ",ap.name aname,acc_img,g.name gname from order_detail" +
                " join accompanying_play ap on ap.id = accompanying_play_id" +
                " join game g on ap.game_id = g.id" +
                " where order_id not in (select order_id from order_detail where rownum <= ?) and rownum <= ?";
        try {
            return queryRunner.query(sql, new ResultSetHandler<List<Order_details>>() {
                @Override
                public List<Order_details> handle(ResultSet rs) throws SQLException {
                    while (rs.next()) {
                        list.add(new Order_details(rs.getInt("order_id"),
                                new Section(rs.getString("aname"),
                                        rs.getString("acc_img"),
                                        new Game(rs.getString("gname"))),
                                rs.getInt("play_time"),
                                rs.getInt("order_price")));
                    }
                    return list;
                }
            }, (page - 1) * size, size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<Order_details> getAll () {
        List<Order_details> list = new LinkedList<>();
        String sql = "select order_id, accompanying_play_id, play_time, order_price" +
                ",ap.name aname,acc_img,g.name gname from order_detail" +
                " join accompanying_play ap on ap.id = accompanying_play_id" +
                " join game g on ap.game_id = g.id";
        try {
            return queryRunner.query(sql, new ResultSetHandler<List<Order_details>>() {
                @Override
                public List<Order_details> handle(ResultSet rs) throws SQLException {
                    while (rs.next()) {
                        list.add(new Order_details(rs.getInt("order_id"),
                                new Section(rs.getString("aname"),
                                        rs.getString("acc_img"),
                                        new Game(rs.getString("gname"))),
                                rs.getInt("play_time"),
                                rs.getInt("order_price")));
                    }
                    return list;
                }
            });
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public long getPageSize(int size) {
        String sql = "select count(1) from order_detail";
        try {
            double res = Double.valueOf(queryRunner.query(sql,new ScalarHandler<>()).toString());
            return (long)Math.ceil(res/size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int getStateCount(){
        try {
            String sql = "select count(1) from order_detail where state = 1";
            return queryRunner.query(sql,new ScalarHandler<BigDecimal>()).intValue();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public boolean update(){
        try {
            String sql = "update order_detail\n" +
                    "   set state = 0";
            return queryRunner.execute(sql)>=1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
